<#include "/ftl/lib/operations.ftl"/>
<div class="easyui-layout" fit="true">  
	<div region="north" style="padding:5px;border:1px solid #ddd; background-color: #f4f4f4" id="global_${action_id}"  >
	</div>  
	<div region="center" border="false">
		<div class="easyui-panel"  data-options="href:'${request.contextPath}${namespace?default('')}/mp/auth/admin-list.htm?action_id=${action_id}',fit:true">
		</div>
	</div>
</div>
<script type="text/javascript">

var myId_${action_id} ='';
<#if mine?exists>
myId_${action_id} = '${mine.id?default('')}';
</#if>

function add_${action_id}(){
	$('<div/>').dialog({
		href:'${request.contextPath}${namespace?default('')}/mp/auth/admin-form.htm?action_id=${action_id}',
		width:500,   
		height:200,   
		title: '${action.getText('form.add.title')}',
		modal:true,
		maximizable:false,
		collapsible:false,
		minimizable:false,
		resizable:false,
		buttons:[{iconCls : 'icon-ok',
					text:'${action.getProperty('net.esj.background.message','bg.datagrid.save')}',
					handler:function(){
						var d = $(this).closest('.window-body');
						$('#form_${action_id}').form('submit', {
							url : '${request.contextPath}${namespace?default('')}/mp/auth/admin-add.json',
							onSubmit: function(){
								var p1 = $(this).find('[name="entity.password"]').val();
								var p2 = $(this).find('[name="repassword"]').val();
								if(p1!=p2){
									$.messager.alert('${action.getProperty('net.esj.background.message','bg.message.error')}','${action.getText('form.validate.twice.pwd.error')}','error');
									return false;
								}
								return $(this).form('validate');
	        				},
							success : function(json) {
								eval("json="+json);
								if(json.level!='error'){
									$.messager.alert(json.title,json.msg,json.level);
									d.dialog('destroy');
									$("#datagrid_${action_id}").datagrid('reload');
								}else{
									$.messager.alert(json.title,json.msg,json.level);
								}
							}
						});
					}},
					{iconCls : 'icon-undo',
					text:'${action.getProperty('net.esj.background.message','bg.datagrid.reset')}',
					handler:function(){
						$('#form_${action_id}').form('clear');
					}}],
		onClose : function() {
			$(this).dialog('destroy');
		}
	});
}

function edit_${action_id}(data) {
	$('<div>').dialog({
		href : '${request.contextPath}${namespace?default('')}/mp/auth/admin-edit.htm?action_id=${action_id}',
		width:500,   
		height:200,   
		modal : true,
		title : '${action.getText('form.edit.title')}',
		buttons : [ {
			text : '${action.getProperty('net.esj.background.message','bg.datagrid.edit')}',
			iconCls : 'icon-edit',
			handler : function() {
				var d = $(this).closest('.window-body');
				$('#edit_${action_id}').form('submit', {
					url : '${request.contextPath}${namespace?default('')}/mp/auth/admin-update.json',
					onSubmit: function(){
						var p1 = $(this).find('[name="entity.password"]').val();
						var p2 = $(this).find('[name="repassword"]').val();
						if(p1!=p2){
							$.messager.alert('${action.getProperty('net.esj.background.message','bg.message.error')}','${action.getText('form.validate.twice.pwd.error')}','error');
							return false;
						}
						return $(this).form('validate');
    				},
					success : function(json) {
						eval("json="+json);
						$.messager.alert(json.title,json.msg,json.level);
						d.dialog('destroy');
						$("#datagrid_${action_id}").datagrid('reload');
					}
				});
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		},
		onLoad : function() {
			$('#edit_${action_id}').form('load', '${request.contextPath}${namespace?default('')}/mp/auth/admin-load.json?id='+data.id+'&n='+Math.random());
		}
	});
}

function delete_${action_id}(data){
	if(check_mine_${action_id}){
		return;
	}
	$.messager.confirm('${action.getProperty('net.esj.background.message','bg.message.info')}', '${action.getProperty('net.esj.background.message','bg.datagrid.delete.ask')}', function(b) {
		if (b) {
			$.ajax({
				url : '${request.contextPath}${namespace?default('')}/mp/auth/admin-delete.json',
				data:{deletes : data.id},
				dataType : 'JSON',
				success : function(json) {
					$.messager.alert(json.title,json.msg,json.level);
					$("#datagrid_${action_id}").datagrid('reload');
					$('#datagrid_${action_id}').datagrid('clearSelections');
				}
			});
		}
	});
}

function grant_${action_id}(data){
	if(check_mine_${action_id}){
		return;
	}
	$('<div>').dialog({
		href : '${request.contextPath}${namespace?default('')}/mp/auth/admin-grant.htm?action_id=${action_id}',
		width:400,   
		height:300,   
		modal : true,
		title : '${action.getText('form.grant.title')}',
		buttons : [ {
			text : '${action.getProperty('net.esj.background.message','bg.datagrid.edit')}',
			iconCls : 'icon-edit',
			handler : function() {
				var d = $(this).closest('.window-body');
				$('#grant_${action_id}').form('submit', {
					url : '${request.contextPath}${namespace?default('')}/mp/auth/admin-doGrant.json',
					onSubmit: function(){
						var nodes = $('#grant_${action_id}').find('#funcTree').tree('getChecked');
						var ids = [] ;
						for(var i=0;i<nodes.length;i++){
							ids.push(nodes[i].id);
						}
						$('#grant_${action_id}').find('[name="entity.funcs"]').val(ids.join(';'));
						
						var gids =[];
						$.each($('#grant_${action_id}').find('[name="groups"]'),function(i,d){
							if($(d).attr('checked')){
								gids.push($(d).val());
							}
						}); 
						$('#grant_${action_id}').find('[name="entity.grs"]').val(gids.join(';'));
						
						return $(this).form('validate');
    				},
					success : function(json) {
						eval("json="+json);
						$.messager.alert(json.title,json.msg,json.level);
						d.dialog('destroy');
						$("#datagrid_${action_id}").datagrid('reload');
					}
				});
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		},
		onLoad : function() {
			$('#grant_${action_id}').find('#funcTree').tree({
				url:'${request.contextPath}${namespace?default('')}/mp/auth/func-mygrid.json?model.id=${model?default('')}',
				onLoadSuccess:function(){
					$.get('${request.contextPath}${namespace?default('')}/mp/auth/admin-load.json?id='+data.id+'&n='+Math.random(),{},function(data){
						eval("data="+data);
						$('#grant_${action_id}').find('[name="entity.id"]').val(data["entity.id"]);
						for(var i = 0;i<data["entity.funcs"].length;i++){
							var node = $('#grant_${action_id}').find('#funcTree').tree('find',data["entity.funcs"][i]);
							if(node){
								$('#grant_${action_id}').find('#funcTree').tree('check',node.target);
							}
						}
						for(var i = 0;i<data["entity.grs"].length;i++){
							$('#grant_${action_id}').find('[name="groups"][value="'+data["entity.grs"][i]+'"]').attr('checked','checked');
						}
					});
				}
			});
			
		}
	});
}

function close_${action_id}(data){
	if(check_mine_${action_id} || data.state==0){
		return;
	}
	$.messager.confirm('', '${action.getText('admin.freeze.warning')}', function(b) {
		if (b) {
			$.ajax({
				url : '${request.contextPath}${namespace?default('')}/mp/auth/admin-state.json',
				data:{id : data.id,state:0},
				dataType : 'JSON',
				success : function(json) {
					$.messager.alert(json.title,json.msg,json.level);
					$("#datagrid_${action_id}").datagrid('reload');
					$('#datagrid_${action_id}').datagrid('clearSelections');
				}
			});
		}
	});
}

function open_${action_id}(data){
	if(check_mine_${action_id} || data.state==1){
		return;
	}
	$.messager.confirm('', '${action.getText('admin.unfreeze.warning')}', function(b) {
		if (b) {
			$.ajax({
				url : '${request.contextPath}${namespace?default('')}/mp/auth/admin-state.json',
				data:{id : data.id,state:1},
				dataType : 'JSON',
				success : function(json) {
					$.messager.alert(json.title,json.msg,json.level);
					$("#datagrid_${action_id}").datagrid('reload');
					$('#datagrid_${action_id}').datagrid('clearSelections');
				}
			});
		}
	});
}

function check_mine_${action_id}(data){
	if(data.id==myId_${action_id}){
		return false;
	}
	return true;
}

</script>
<@operations contextUri='mp/auth/admin' />